<?php

namespace api\zmt\controller;

use cmf\controller\RestBaseController;
use think\facade\Request;

class AddressController extends RestBaseController
{
    public function getAddress()
    {
        $keyword = input('get.keyword','');
        $where = [];
        if ($keyword) {
            $where[] = ['id', 'like', '%' . $keyword . '%'];
        }

        //页码
        $page = input('page/d',1);
        //每页返回的条数
        $pageSize = input('page_size/d',10);
        $list = \api\zmt\model\AddressModel::where($where)
            ->order('list_order','desc')
            ->field('id,name,location,address')
            ->page($page,$pageSize)
            ->select();

        $this->success('收货地址表', $list);
    }

    // 新增的修改收货地址的方法
    public function updateAddress()
    {
        $data = Request::post();

        $user = \api\zmt\model\AddressModel::where('mobile', $data['mobile'])->find();
        if (!$user) {
            return json(['status' => 'error', 'message' => '用户不存在']);
        }

        try {
            $user->mobile = $data['mobile'];
            $user->detail = $data['detail'];
            $user->address = $data['address'];
            $user->save();
            return json(['status' => 'success', 'message' => '个人信息更新成功', 'data' => $user->toArray()]);
        } catch (\Exception $e) {
            return json(['status' => 'error', 'message' => $e->getMessage()]);
        }
    }


}